package cn.nuomi.shop.api.dao.impl;

import cn.nuomi.shop.api.dao.GoodsDao;
import cn.nuomi.shop.api.entity.Goods;
import cn.nuomi.shop.api.util.DBUtil;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.*;
import java.util.List;


/**
 * @date: 2021/4/27 10:28
 * @description:
 */
public class GoodsDaoImpl implements GoodsDao {

    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());

    @Override
    public List<Goods> getGoods() {
        String sql = "SELECT * FROM goods";
        //开启驼峰转换
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);

        //执行SQL，填充参数，转换结果
        BeanListHandler<Goods> beanHandler = new BeanListHandler(Goods.class, processor);

        try{
            List<Goods> goodsList = queryRunner.query(sql, beanHandler);
            //goodsList.forEach(item-> System.out.println(item));
            return goodsList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Goods> getPhone() {
        String sql = "SELECT * FROM phoneGoods";
        //开启驼峰转换
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);

        //执行SQL，填充参数，转换结果
        //获取手机的商品信息

        BeanListHandler<Goods> beanHandler = new BeanListHandler(Goods.class, processor);

        try{
            List<Goods> phoneList = queryRunner.query(sql, beanHandler);
            return phoneList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Goods> getIntelligence() {
        String sql = "SELECT * FROM intelligenceGoods";
        //开启驼峰转换
        BeanProcessor bean = new GenerousBeanProcessor();
        RowProcessor processor = new BasicRowProcessor(bean);


        BeanListHandler<Goods> beanHandler = new BeanListHandler(Goods.class, processor);

        try{
            List<Goods> intelligenceList = queryRunner.query(sql, beanHandler);
            return intelligenceList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }



}
